package fun.codedesign.yinxue.code.php2java.model;

import fun.codedesign.yinxue.code.php2java.rule.Php2JavaRuleChain;
import fun.codedesign.yinxue.code.php2java.rule.statement.ImportStatementRule;
import fun.codedesign.yinxue.code.php2java.rule.statement.NamespaceStatementRule;
import fun.codedesign.yinxue.util.batch.BatchFileUtil;
import org.antlr.v4.runtime.TokenStreamRewriter;
import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.tree.ParseTree;
import org.yinxue.swing.unit.model.ClassDesc;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Php2JavaContext {
    public Php2JavaRuleChain wordChain = Php2JavaRuleChain.WORD_CHAIN;

    public Php2JavaRuleChain statementChain = Php2JavaRuleChain.STATMENT_CHAIN;

    public Php2JavaRuleChain namespaceChain = Php2JavaRuleChain.chain(NamespaceStatementRule.class);

    public Php2JavaRuleChain importChain = Php2JavaRuleChain.chain(ImportStatementRule.class);

    public PhpDesc phpDesc = new PhpDesc();
    public ClassDesc javaDesc = new ClassDesc();
    public StringBuilder builder = new StringBuilder();

    public ParseTree phpBlockContext;

    public Stack<List> stack = new Stack<>();
    public List<List> methodBodyList = new ArrayList<>();

    public List<String> cache = new ArrayList<>();

    public Status status;

    public TokenStreamRewriter writer;

    public void switchStatus(Status status) {
        this.status = status;
    }

    public boolean isMethodLineStatus() {
        return this.status == Status.MEHOD_LINE_START || this.status==Status.MEHOD_LINE_END;
    }

    public boolean isClassVariableStatus() {
        return this.status == Status.CLASS_VARIABLE_START;
    }

    public boolean isClassConstrutorStatus() {
        return this.status == status.CLASS_CONSTRUCTOR_START;
    }

    public boolean isClassMethodStatus() {
        return this.status == status.CLASS_METHOD_START;
    }

    public boolean createJavaFile(String targetPath) {
        try{
            if (javaDesc.packagePath == null) {
                // 部分php脚本是没有 packagePath的
                // TODO
                return false;
            }
            String path = targetPath+"\\"+javaDesc.packagePath.replace(".","\\") +"\\"+javaDesc.simpleName +".java";
            BatchFileUtil.createTargetFile(path, getText());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public String mapNamespace(String namespace) {
        phpDesc.namespace = namespace;
        javaDesc.packagePath = namespaceChain.invokeRules(phpDesc.namespace);
        return javaDesc.packagePath;
    }

    public String mapUse(String originPath) {
        String importLine = wordChain.useReplaceSlashWithDot(originPath);
        String targetImportLine = importChain.invokeRules(originPath);
        javaDesc.sourceImportLines.put(importLine, importLine);
        return targetImportLine;
    }

    public String mapClassName(String originTypeName) {
        String newTypeName = wordChain.typeName2UpperCaseFirstChar(originTypeName);
        phpDesc.className = originTypeName;
        javaDesc.simpleName = newTypeName;
        return newTypeName;
    }


    public void mapConstruct() {

    }

    public String getText() {
        String text = writer.getText("default", Interval.of(2, writer.getTokenStream().size()));
        return text;
    }
}
